FROM alpine:3.10

# 主nginx nginx-tengine

# study from: https://www.cnblogs.com/rebackNow/p/12683079.html
# nginx: https://git.io/vSIyj


WORKDIR /usr/src

RUN rm -rf /var/cache/apk/* && \
    rm -rf /tmp/*

ENV TENGINE_VERSION 2.3.2
ENV CONFIG "\
        --prefix=/etc/nginx \
        --sbin-path=/usr/sbin/nginx \
        --modules-path=/usr/src/tengine-$TENGINE_VERSION/modules \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --pid-path=/var/run/nginx.pid \
        --lock-path=/var/run/nginx.lock \
        --user=nginx \
		--add-module=modules/ngx_http_upstream_session_sticky_module \
		--add-module=modules/ngx_http_upstream_check_module"

RUN     addgroup -S nginx \
        && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
        && addgroup -g 82 -S www-data && adduser -u 82 -D -S -G www-data www-data \
        && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
        && apk add --no-cache --virtual .build-deps \
                gcc \
                libc-dev \
                make \
                openssl-dev \
                pcre-dev \
                zlib-dev \
                linux-headers \
                curl \
                libxslt-dev \
                gd-dev \
                geoip-dev \
                tar
RUN curl -L "http://tengine.taobao.org/download/tengine-$TENGINE_VERSION.tar.gz" -o tengine-$TENGINE_VERSION.tar.gz \
        && tar -zxvf tengine-$TENGINE_VERSION.tar.gz \
        && rm tengine-$TENGINE_VERSION.tar.gz \
        && cd /usr/src/tengine-$TENGINE_VERSION \
        && sh ./configure $CONFIG \
        && make -j$(getconf _NPROCESSORS_ONLN) \
        && make -j$(getconf _NPROCESSORS_ONLN) \
        && make install \
        && rm -rf /etc/nginx/html/ \
        && mkdir /etc/nginx/conf.d/ \
        && strip /usr/sbin/nginx* \
        && rm -rf /usr/src/tengine-$TENGINE_VERSION \
        #&& curl -L "http://file.sxycy.cn:88/vod/download/nginx-rtmp/nginx.conf" -o /etc/nginx/nginx.conf \
        \
        # Bring in gettext so we can get `envsubst`, then throw
        # the rest away. To do this, we need to install `gettext`
        # then move `envsubst` out of the way so `gettext` can
        # be deleted completely, then move `envsubst` back.
        && apk add --no-cache --virtual .gettext gettext \
        && mv /usr/bin/envsubst /tmp/ \
        \
        && runDeps="$( \
                scanelf --needed --nobanner --format '%n#p' /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
                        | tr ',' '\n' \
                        | sort -u \
                        | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
        )" \
        && apk add --no-cache --virtual .nginx-rundeps $runDeps \
        && apk del .build-deps \
        && apk del .gettext \
        && mv /tmp/envsubst /usr/local/bin/ \
        \
        # Bring in tzdata so users could set the timezones through the environment
        # variables
        && apk add --no-cache tzdata \
        \
        # forward request and error logs to docker log collector
        && ln -sf /dev/stdout /var/log/nginx/access.log \
        && ln -sf /dev/stderr /var/log/nginx/error.log


#COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

STOPSIGNAL SIGTERM

CMD ["nginx"]


# 构建
# sudo docker build -t weiguoxin/tengine .
# 运行 主
# sudo docker run -d --net=host --privileged=true --restart=always --name 'nginx' -v /opt/app/mind/web/:/app -v /opt/app/mind/docker/nginx/nginx.conf:/etc/nginx/nginx.conf weiguoxin/tengine

